<?php
/**
 * Created by PhpStorm.
 * User: RatuoWeb1
 * Date: 2019/2/27
 * Time: 21:08
 */

namespace app\admin\validate;
use think\facade\Request;
use think\Validate;

class Common extends Validate
{
    /**
     *  验证器说明
     * 1,获取验证器规则中的参数
     * 2,如果验证不通过直接返回前端
     */
    public function goCheck(){
        $paramData = $this -> getParamData();
        $res = $this -> check($paramData,$this -> rule);
        if(!$res){
            $this -> echoErrorResult($this -> getError());
        }
    }

    /**
     * 获取当前用户传递的数据过滤为需要验证的参数
     * @return mixed
     */
    private function getParamData(){
        $paramData = Request::param();
        //获取验证中需要验证的参数
        $rule_keys = array_keys($this -> rule);
        $validata = array();
        foreach ($paramData as $key => $val){
            if(in_array($key,$rule_keys)){
                $validata[$key] = $val;
            }
        }
        return $validata;
    }

    /**
     * 返回用户错误的填写信息
     * @param $error
     */
    private function echoErrorResult($error){
        echo echojson(0,$error);exit();
    }

}
